﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OutdoorLib.BaseEntity.Dictionary;
using Grishko.DataTier;
using OutdoorLib.BaseEntity.WS;
using OutdoorLib.BaseEntity.Agency;
using Grishko.Tools;

/// <summary>
/// Редактирование древовидного справочника
/// Вызывается с TypeID - по TypeID- отыскивается справочник данного типа - редирект и уже работа с ID
/// </summary>
public partial class iFrameDictionary_TreeDictionaryEditor : System.Web.UI.Page
{
    readonly string qTypeID = "TypeID";
    readonly string qID = "ID";
    readonly string vDepartment = "DepartmentTree";
    

    public string DictionaryTypeID { get { return Request.QueryString[qTypeID]; } }

    public string FilterID { get{return Request.QueryString[qID]; } 
    }

    private bool IsDeprtmentTree
    { 
        get{
            if (!(String.IsNullOrEmpty(FilterID))) 
                                           return false;

            return DictionaryTypeID.ToUpper ()==vDepartment.ToUpper ();
        }
    }

    public TreeDictionaryItem _Dictionary 
    {
        get 
        {
            if (IsDeprtmentTree)
                        return AppEnvironment.CurrentAgency.Department;

            
            TreeDictionaryItem i = null;
            if (String.IsNullOrEmpty(FilterID))
            {
                

                i = AppEnvironment.DictionarySource.GetTreeDictionaryItem(DictionaryTypeID);
                if (null == i)
                    i = AppEnvironment.DictionarySource.CreateTreeDictionaryItem(DictionaryTypeID);

                QueryStringTools.RefreshPage(qID+"="+ i.ID);
            }
            else
                i = AppEnvironment.DictionarySource.GetTreeDictionaryItemWithID(FilterID);
            return i;
        }

    }

    

    protected void Page_Load(object sender, EventArgs e)
    {
        tvDictionaryTree.OnSelectedNodeChanged = OnNodeChange;
        tvDictionaryTree.OnRemoveNode = OnRemoveNode;
        tvDictionaryTree.OnAddNode = OnAddNode;


        if (!IsPostBack)
        {
            MyBind();
        }
        
    }
    private bool OnAddNode(string NodeID)
    {
        DictionaryItem s_nd = (DictionaryItem)_Dictionary.Find(NodeID);        
        if (IsDeprtmentTree)
        {
            Department dchild = new Department() { Name ="Новый депратамент"};
            s_nd.ChildNodes.Add(dchild);
            OnNodeChange(dchild.ID);
            wsAgency.SaveAgency();
        }

        else
        {
            TreeDictionaryItem.BranchItem child = new TreeDictionaryItem.BranchItem() { ParamID = Guid.NewGuid().ToString(), Value = "Новый элемент" };
            s_nd.ChildNodes.Add(child);
            OnNodeChange(child.ID);
            AppEnvironment.DictionarySource.Save(_Dictionary);
        }
        
        return true;
    }
    private bool OnRemoveNode(string NodeID)
    {
        DictionaryItem s_nd = (DictionaryItem)_Dictionary.Find(NodeID);
        if (null == s_nd.ParentNode)
        {
            MyBind();
            return true;
        }
        DictionaryItem p = (s_nd.ParentNode as DictionaryItem);
        p.ChildNodes.Remove(s_nd);
        AppEnvironment.DictionarySource.Save(_Dictionary);
        MyBind();
        return true;
    }


    private bool OnNodeChange(string NodeID)
    {
        string s = NodeID;
        tvDictionaryTree.SelectedNodeID = s;
        DictionaryItem tr =(DictionaryItem)_Dictionary.Find(s);
        txtNodeVal.Text = tr.Value.ToString () ;
        MyBind();
        return true;
    }

    private void MyBind()
    {   
        tvDictionaryTree.DataSource = _Dictionary;
        tvDictionaryTree.DataBind();


    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }
    protected void OnSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(tvDictionaryTree.SelectedNodeID))
        {
            MyBind();
            return;
        }
        
        DictionaryItem tr = (DictionaryItem)_Dictionary.Find(tvDictionaryTree .SelectedNodeID);
        tr.Value = txtNodeVal.Text;
        
        if (IsDeprtmentTree)
            wsAgency.SaveAgency();
        else
            AppEnvironment.DictionarySource.Save(_Dictionary);

        MyBind();
    }
}
